﻿using System;
using System.Windows;

namespace BluEtch.GotPhone.Helpers
{
    public class Threading
    {
        /// <summary>
        /// Executes an action on the UI thread. If it is called directly from the UI thread,
        /// the action is executed immediately. If it is not, it will be queued to run on the 
        /// UI thread's dispatcher and thus executed asynchronously.
        /// </summary>
        /// <param name="action">Action to be executed on the UI thread.</param>
        public static void InvokeUI(Action action)
        {
            // Note that althought CheckAccess() does not appear in the Intellisense, 
            //  it is in fact present and will work.
            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(action);
            }
        }
    }
}
